package com.xiaotu.day01;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * 点菜系统
 */
public class DianCaiDemo {
    static List<DianCai> caiList = new ArrayList<>();
    static List<DianCai> yiDianCaiList = new ArrayList<>();
    public static void main(String[] args) {
        inintMenu();
        //System.out.println(caiList);
        Scanner scanner = new Scanner(System.in);
        while (true){
            showMenus();
            System.out.println("请输入编号:");
            int nextInt = scanner.nextInt();
            if(nextInt == 1){
                //展示菜单
                while (true){
                for (DianCai dianCai:caiList) {
                    System.out.println(dianCai.getId()+"\t\t"+dianCai.getName()+"\t\t"+ dianCai.getPice());
                }
                //点菜功能
                System.out.println("-----输入序号点菜,输入0返回上一级-----");
                    int anInt = new Scanner(System.in).nextInt();
                    if(anInt!=0){
                        addYiDian(anInt);
                    }if(anInt==0){
                        break;
                    }
                }
            }else if (nextInt == 2 ){
                //展示已点菜单
                if (yiDianCaiList.size()==0){
                    System.out.println("您还未点菜,请先点菜!");
                }else{
                    System.out.println("你点的菜品如下所示:");
                    for (DianCai dianCai:yiDianCaiList) {
                        System.out.println(dianCai.getId()+"\t\t"+dianCai.getName()+"\t\t"+ dianCai.getPice());
                    }
                    while (true){
                        System.out.println("-----输入0返回上一级-----");
                        int anInt = new Scanner(System.in).nextInt();
                        if(anInt==0){
                            break;
                        }
                    }
                }
            }else if(nextInt == 3){
                //买单
                double sum = 0.00;
                for (DianCai dianCai:yiDianCaiList) {
                    sum += dianCai.getPice();
                }
                System.out.println("本次消费:"+sum);
                yiDianCaiList.clear();
            }else if(nextInt == 4){
                //添加菜品
                System.out.println("请输入菜品名称:");
                String name = new Scanner(System.in).nextLine();
                System.out.println("请设定菜品价格:");
                Double price = new Scanner(System.in).nextDouble();
                addMenu(name,price);
            }else if(nextInt ==0){
                System.out.println("******系统关闭成功******");
                break;
            }else{
                System.out.println("输入错误,请重新选择输入!");
            }
        }
    }
    public static void showMenus(){
        System.out.println("******小土点菜系统******");
        System.out.println("----主菜单----");
        System.out.println("菜单\t\t\t 1");
        System.out.println("已点菜品\t\t 2");
        System.out.println("买单\t\t\t 3");
        System.out.println("添加菜品\t\t 4");
        System.out.println("退出系统\t\t 0");
        System.out.println("根据编号选择服务");
    }
    public static List<DianCai> inintMenu(){
        caiList.add(new DianCai(1, "香辣肉丝", 29.00));
        caiList.add(new DianCai(2, "炖王八", 89.90));
        caiList.add(new DianCai(3, "黄焖鸡", 30.00));
        caiList.add(new DianCai(4, "新疆大盘鸡", 88.88));
        return caiList;
    }
    public static void addMenu(String name,Double price){
        int id = caiList.size()+1;
        caiList.add(new DianCai(id,name,price));
        System.out.println("添加成功,您可以返回菜单查看");
    }
    public static void addYiDian(Integer id){
        if(id>caiList.size()){
            System.out.println("未找到对应菜品请重新选择");
            return;
        }else if(id<=caiList.size()){
            for (DianCai dianCai:caiList) {
                if (dianCai.getId()==id){
                    if(yiDianCaiList.size()>0){
                        for (DianCai cai : yiDianCaiList) {
                            if(cai.getId() == id){
                                System.out.println("您已经点过该菜品!请重新点菜!");
                                break;
                            }else{
                                yiDianCaiList.add(dianCai);
                                System.out.println("您点了一份\t"+dianCai.getName()+"\t!");
                                break;
                            }
                        }
                    }else{
                        yiDianCaiList.add(dianCai);
                        System.out.println("您点了一份\t"+dianCai.getName()+"\t!");
                    }
                    break;
                }
            }
        }
    }
}
